//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FarlieGumbelMorgensternCopula.h"
using namespace Cephei::QL::Math::Copulas;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (Double theta) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phFarlieGumbelMorgensternCopula = NULL;
#endif
        QuantLib::Real _theta = (QuantLib::Real)ValueHelper::Convert (theta); //d
        _ppFarlieGumbelMorgensternCopula = new boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula> (new QuantLib::FarlieGumbelMorgensternCopula ( _theta ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFarlieGumbelMorgensternCopula = NULL;
#endif
	_ppFarlieGumbelMorgensternCopula = &childNative;
    
}
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (QuantLib::FarlieGumbelMorgensternCopula& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFarlieGumbelMorgensternCopula = NULL;
#endif
	_ppFarlieGumbelMorgensternCopula = new boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula> (&childNative);
    
    _FarlieGumbelMorgensternCopulaOwner = owner;
    
}

Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (CFarlieGumbelMorgensternCopula^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFarlieGumbelMorgensternCopula = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFarlieGumbelMorgensternCopula = new boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFarlieGumbelMorgensternCopula = NULL;
#endif
	if (!t->IsSubclassOf(CFarlieGumbelMorgensternCopula::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (QuantLib::Handle<QuantLib::FarlieGumbelMorgensternCopula>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFarlieGumbelMorgensternCopula = &childNative;
	_ppFarlieGumbelMorgensternCopula = &static_cast<boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula>>(childNative.currentLink());
    
    _FarlieGumbelMorgensternCopulaOwner = owner;
}
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (QuantLib::Handle<QuantLib::FarlieGumbelMorgensternCopula> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFarlieGumbelMorgensternCopula = &childNative;
	_ppFarlieGumbelMorgensternCopula = &static_cast<boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::CFarlieGumbelMorgensternCopula (QuantLib::FarlieGumbelMorgensternCopula childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFarlieGumbelMorgensternCopula = NULL;
#endif
	_ppFarlieGumbelMorgensternCopula = new boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula> (new QuantLib::FarlieGumbelMorgensternCopula (childNative));
    
}
#endif

Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::~CFarlieGumbelMorgensternCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFarlieGumbelMorgensternCopula != NULL)
    {
	    delete _ppFarlieGumbelMorgensternCopula;
        _ppFarlieGumbelMorgensternCopula = NULL;
    }
}
Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::!CFarlieGumbelMorgensternCopula ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFarlieGumbelMorgensternCopula != NULL)
    {
	    delete _ppFarlieGumbelMorgensternCopula;
    }
}
QuantLib::FarlieGumbelMorgensternCopula& Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::GetReference ()
{
    if (_ppFarlieGumbelMorgensternCopula == NULL) throw REFNEW NativeNullException ();
	return **_ppFarlieGumbelMorgensternCopula;
}
boost::shared_ptr<QuantLib::FarlieGumbelMorgensternCopula>& Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::GetShared ()
{
    if (_ppFarlieGumbelMorgensternCopula == NULL) throw REFNEW NativeNullException ();
	return *_ppFarlieGumbelMorgensternCopula;
}
QuantLib::FarlieGumbelMorgensternCopula* Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::GetPointer ()
{
    if (_ppFarlieGumbelMorgensternCopula == NULL) throw REFNEW NativeNullException ();
	return &**_ppFarlieGumbelMorgensternCopula;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FarlieGumbelMorgensternCopula>& Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::GetHandle ()
{
	if (_phFarlieGumbelMorgensternCopula == NULL)
	{
		_phFarlieGumbelMorgensternCopula = new Handle<QuantLib::FarlieGumbelMorgensternCopula> (*_ppFarlieGumbelMorgensternCopula);
	}
	return *_phFarlieGumbelMorgensternCopula;
}
#endif
bool Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula::HasNative () 
{
	return (_ppFarlieGumbelMorgensternCopula != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Copulas::IFarlieGumbelMorgensternCopula^ Cephei::QL::Math::Copulas::CFarlieGumbelMorgensternCopula_Factory::Create (Double theta)
{
    return REFNEW CFarlieGumbelMorgensternCopula ( theta);
}
